//
//  UIViewController+JQRouter.m
//  JQKit
//
//  Created by JQZ on 2018/1/10.
//  Copyright © 2018年 JQZ. All rights reserved.
//

#import "UIViewController+JQRouter.h"
#import <objc/runtime.h>

#pragma mark - UIViewController Category
@implementation UIViewController (JQRouter)

static char kAssociatedParamsObjectKey;

- (void)setParams:(NSDictionary *)params
{
    objc_setAssociatedObject(self, &kAssociatedParamsObjectKey, params, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSDictionary *)params
{
    return objc_getAssociatedObject(self, &kAssociatedParamsObjectKey);
}

+ (instancetype)controllerForRouterParams:(NSDictionary *)params
{
    SEL CONTROLLER_SELECTOR = sel_registerName("initWithRouterParams:");
    UIViewController *controller = nil;
    Class controllerClass = [self class];
    if ([[self class] instanceMethodForSelector:CONTROLLER_SELECTOR]){
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        controller = [[controllerClass alloc] performSelector:CONTROLLER_SELECTOR withObject:params];
#pragma clang diagnostic pop
    }
    else {
        controller = [[controllerClass alloc] initWithNibName:nil bundle:nil];
        controller.params = params;
    }
    return controller;
}

@end
